/**
 * Load the module and the widgets
 * 
 * @author h.r.inc
 */
package com.rayenterprise.kms.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.rayenterprise.kms.domain.KInfo;
import com.rayenterprise.kms.widgets.InfoNode;
import com.rayenterprise.kms.widgets.InfoPresenter;
import com.rayenterprise.kms.widgets.LeftTree;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Kms4gwt implements EntryPoint, ResizeHandler {

	private LeftTree tree = new LeftTree();
	private ScrollPanel sp = new ScrollPanel();

	private InfoPresenter presenter;
	final HandlerManager eventBus = new HandlerManager(null);
	private static Kms4gwt singleton;

	public static Kms4gwt get() {
		return singleton;
	}

	public void setInfo(InfoNode theNode) {		
		presenter.setInfo(theNode);
	}
	public void setWidget(Widget w) {
		presenter.setEditingWidget(w);
	}

	@Override
	public void onModuleLoad() {
		singleton = this;
		DockPanel outer = new DockPanel();
		presenter = new InfoPresenter(eventBus);
		VerticalPanel vpCenter = new VerticalPanel();

		presenter.setWidth("100%");
		outer.setWidth("100%");
		sp.setWidget(tree);

		vpCenter.add(presenter);
		outer.add(sp, DockPanel.WEST);
		outer.setCellHeight(sp, "100%");
		
		outer.add(vpCenter, DockPanel.CENTER);

		outer.setSpacing(4);
		outer.setCellWidth(vpCenter, "100%");

		Window.addResizeHandler(this);

		// Get rid of scrollbars, and clear out the window's built-in margin,
		// because we want to take advantage of the entire client area.
		Window.enableScrolling(false);
		Window.setMargin("0px");
		// Finally, add the outer panel to the RootPanel, so that it will be
		// displayed.
		RootPanel.get("holder_main").add(outer);

		// Finally, add the outer panel to the RootPanel, so that it will be

		// Call the window resized handler to get the initial sizes setup. Doing
		// this in a deferred command causes it to occur after all widgets'
		// sizes
		// have been computed by the browser.
		DeferredCommand.addCommand(new Command() {
			public void execute() {
				onWindowResized(Window.getClientWidth(), Window
						.getClientHeight());
			}
		});

		onWindowResized(Window.getClientWidth(), Window.getClientHeight());

	}

	public void onResize(ResizeEvent event) {
		onWindowResized(event.getWidth(), event.getHeight());
	}

	public void onWindowResized(int width, int height) {

		int shortcutHeight = height - sp.getAbsoluteTop() - 8;
		if (shortcutHeight < 1) {
			shortcutHeight = 1;
		}
		sp.setHeight(shortcutHeight + "px");

		
		presenter.adjustSize(width, height);
	}

}
